﻿<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>SharePoint Freshness Boost with XRANK</title>
        <script>
        function CreateXRANK() {
            var constant = document.getElementById("constant").value;
            var maxboost = document.getElementById("maxboost").value;
            var query = document.getElementById("query").value;
            var intervals = document.getElementById("interval").value.split(",");
            var lastRank = 0;
            var queryTest = "my test query";
            for (var i = intervals.length - 1; i >= 0; i--) {
                var rank = maxboost / (1 + (constant * intervals[i]));
                var deltaRank = rank - lastRank;
				
				var decimalString = deltaRank.toString().substr(2);
				var expDeltaRank = decimalString + "E-" + decimalString.length;
				
                query = "(" + query + ")" + " XRANK(cb=" + expDeltaRank + ") " + "write>{ToDay-" + intervals[i] + "}";

                var dateOffset = (24 * 60 * 60 * 1000) * intervals[i];
                var myDate = new Date();
                myDate.setTime(myDate.getTime() - dateOffset);
                var dateString = myDate.getFullYear() + "-" + (myDate.getMonth() + 1) + "-" + myDate.getDate();

                queryTest = "(" + queryTest + ")" + " XRANK(cb=" + expDeltaRank + ") " + "write>" + dateString;

                lastRank = rank;
            }
            query = "{?" + query + "}"; //will not execute if variable is missing
            document.getElementById("result").value = query;
            document.getElementById("resultTest").value = queryTest;
        }
    </script>
    <style>
        .mainbody {
            font-family: "Segoe UI Light", "Segoe UI","Segoe",Tahoma,Helvetica,Arial,sans-serif;
            font-size: 16px;
        }

        h1 {
            font-size: 2em;
            font-weight: normal;
            margin: 0;
            color: #fff !important;
        }

        th {
            background-color: #0072c6;
            color: #fff !important;
            padding-bottom:10px;
            padding-left:15px;
            padding-right:15px;
        }

        button {
            background-color: #0072c6;
            color: #fff;
            font-family: "Segoe UI","Segoe",Tahoma,Helvetica,Arial,sans-serif;
            border: 1px solid #92c0e0;
            padding-left: 10px;
            padding-right: 7px;
            font-size: 1em;
        }
		a, a:visited {color:blue}
    </style>
</head>
<body onload="CreateXRANK()">
<div class="mainbody">
    <table>
        <tr>
            <th colspan="4">
                <h1>SharePoint Freshness Boost XRANK Generator</h1>                
            </th>
            <td rowspan="6" valign="top" style="width:400px;padding-left:10px">
                <p>SharePoint Online does not support the changing of rank models, and the only way to add a freshness boost is by using XRANK.</p>
                <p>
                    By using this tool you can generate a nested XRANK statement which will for an item of the current date, give a constant boost
                    of the value entered in the Max Boost field. It's not as dynamic as the rank component, but by picking sensible intervals you
                    are able to mimic the behavior and get pretty good results.
                </p>
                <p>
                    <i>Note: Only add as many intervals as needed as multiple XRANK statement may have an adverse query time impact.</i>
                </p>
                <p>
                    Reference links:<br />
                    <a href="http://techmikael.blogspot.com/2013/10/adding-freshness-boost-to-sharepoint.html">Tech & Me - Adding freshness boost to SharePoint Online</a>
                    <br />
                    <a href="http://msdn.microsoft.com/en-us/library/sharepoint/dn169052.aspx">MSDN - Customizing ranking models to improve relevance in SharePoint 2013</a>
                </p>
            </td>
        </tr>
        <tr>
            <td>Constant</td>
            <td>
                <input id="constant" type="text" value="0.0333" title="Constant used for logarithmic distribution" /></td>
            <td>Max Boost</td>
            <td>
                <input id="maxboost" type="text" value="1" title="Maximum boost value for todays items" /></td>
        </tr>
        <tr>
            <td>Day intervals</td>
            <td>
                <input id="interval" type="text" value="0,1,7,30,60,90,180,365,580" title="Age intervals in days, comma separated" /></td>
            <td>Query</td>
            <td>
                <input id="query" type="text" value="{searchTerms}" title="Query from Query Rule or test query" /></td>

        </tr>
        <tr>
            <td valign="top">XRANK Query Rule</td>
            <td colspan="3">
                <textarea id="result" rows="10" cols="10" style="width:99%"></textarea></td>
        </tr>
        <tr>
            <td valign="top">XRANK test</td>
            <td colspan="3">
                <textarea id="resultTest" rows="10" cols="10" style="width:99%"></textarea></td>
        </tr>
        <tr>
            <td colspan="4" align="right"><button onclick="CreateXRANK()">Generate</button></td>
        </tr>
    </table>
</div>    
</body>
</html>
